/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Koneksi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.StringTokenizer;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author asus
 */
public class koneksi {

    static Connection conn;
    private static Connection connection;

    public koneksi() {
    }

    public static Connection getDBConnection() throws SQLException, FileNotFoundException, IOException {
        try  {
            String result[] = koneksi.openConfigurationFile(new File("db.conf"));
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL("jdbc:oracle:thin:@" + result[0] + ":1521:XE");
            conn = ds.getConnection(result[1], result[2]);
            System.out.println("Koneksi Berhasil");

        } catch (SQLException ex) {
            System.out.println("GAGAL");

        }
        return conn;
    }

    public static String[] openConfigurationFile(File file) throws FileNotFoundException, IOException {
        FileInputStream input = new FileInputStream(file);
        String teks = new String();
        int c = 0;
        while ((c = input.read()) != -1) {
            teks += (char) c;
        }

        StringTokenizer tokens = new StringTokenizer(teks, ";");
        String result[] = new String[tokens.countTokens()];

        for (int i = 0; i < result.length; i++) {
            result[i] = tokens.nextToken();
        }

        return result;




    }

    public static void main() throws SQLException, FileNotFoundException, IOException {
        koneksi kon = new koneksi();
        kon.getDBConnection();
    }
}
